# Change this to the location of the replication repository
setwd("D://Dropbox//Coalitions//CuesReplication//Results")

### Table 1: Descriptive statistics
dat <- read.csv(".//middesc.csv")
mean(dat$midsession)

summary(glm(midsession ~ chambertenure + cfdist + les + cs, data = dat, 
            subset = which(dat$chambertenure > 0 & dat$commtenure == 0), 
            family = binomial(link = "logit")))

## Robustness of Table 1
summary(glm(midsession ~ chambertenure + cfdist + les + cs, data = dat, 
            subset = which(dat$commtenure == 0),
            family = binomial(link = "logit")))


###

### Table 2: Main Results
load("./cueingresults_105.RData")

## Model 1 of Table 2
# First column: coefficient
# Second column: standard error
# Third column: t-statistic
## 
base.fit <- fit
base.fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(base.fit), base.fit$se, coef(base.fit)/base.fit$se)[c(1:14,475:476),]

# Average pre-treatment agreement rate among connected legislators
weighted.mean(x$agree1[which(x$friend == 1)], x$weights[which(x$friend == 1)])

## Model 2 of Table 2
# First column: coefficient
# Second column: standard error
# Third column: t-statistic
## 
load("./partisan_cueingresults_105.RData")
part.fit <- fit
part.fit$se <- sqrt(diag(dyad.vcov))

cbind(coef(part.fit), part.fit$se, coef(part.fit)/part.fit$se)[c(1:15,476:477),]

## Ceiling effect?
with(x[which(x$copartisans == 1),], weighted.mean(agree1, weights))
with(x[which(x$copartisans == 0),], weighted.mean(agree1, weights))

## The total impact of cue-taking
# Average number of votes changed
coef(base.fit)[5]*weighted.mean(x$n2, x$weights)
# Number of cue-taking opportunities drawn from base data (too large to practicably upload)

### 

### Table 3: Intercameral Differences in Cue-Taking
load("./chamber_cueingresults_105.RData")
cham.fit <- fit
cham.fmla <- fmla
cham.fit$se <- sqrt(diag(dyad.vcov))

cbind(coef(cham.fit), cham.fit$se, coef(cham.fit)/cham.fit$se)[c(1:17,478:479),]
###

### Table 4: The Robustness of Cue-Taking to Recent Changes in Congress
load("./time_cueingresults_105.RData")
time.fit <- fit
time.fit$se <- sqrt(diag(dyad.vcov))

cbind(coef(time.fit), time.fit$se, coef(time.fit)/time.fit$se)[c(1:17,478:479),]
###

### Figure 1: Partisan distribution of connected legislators
connectx <- read.csv(".//connecttotal.csv")

library(ggplot2)
library(gridExtra)
p1 <- ggplot(data = connectx[which(connectx$chamber == "h"),],
             aes(x = congress, y = co)) + geom_line() +
  geom_line(aes(x = congress, y = opp), linetype = "dashed") + 
  ylab("Number of Connections") + xlab("Congress") + 
  annotate(geom="text", x=98.5, y = 4000, label = "Opposite Party") + 
  annotate(geom="text", x =100, y = 1500, label = "Same Party") + 
  ggtitle("House") + theme(plot.title = element_text(hjust = 0.5))

p2 <- ggplot(data = connectx[which(connectx$chamber == "s"),],
             aes(x = congress, y = co)) + geom_line() +
  geom_line(aes(x = congress, y = opp), linetype = "dashed") + 
  ylab("Number of Connections") + xlab("Congress") + 
  annotate(geom="text", x=98, y = 1500, label = "Opposite Party") + 
  annotate(geom="text", x =100, y = 500, label = "Same Party") + 
  ggtitle("Senate") + theme(plot.title = element_text(hjust = 0.5))

grid.arrange(p1, p2)

with(connectx[which(connectx$chamber == "h"),], co/opp)
with(connectx[which(connectx$chamber == "s"),], co/opp)


par(mfrow=c(2,1))
with(connectx[which(connectx$chamber == "h"),], plot(congress, co + opp, ylim = c(0, 10000), type = "l"))
with(connectx[which(connectx$chamber == "h"),], lines(congress, co, lty = "dashed"))
with(connectx[which(connectx$chamber == "h"),], lines(congress, opp, lty = "dotted"))

with(connectx[which(connectx$chamber == "s"),], plot(congress, co + opp, ylim = c(0, 4000), type = "l"))
with(connectx[which(connectx$chamber == "s"),], lines(congress, co, lty = "dashed"))
with(connectx[which(connectx$chamber == "s"),], lines(congress, opp, lty = "dotted"))
###

### Table 5: Reverse cue-giving
load("./reverse_cueingresults_105.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:18,479:480),]
###

### Table 6: Similiarity
load("./buddy_cueingresults_105.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:16),]
###

### Table 7: Exit analysis
load("./exit_cueingresults_105.RData")
exit_fit$se  <- sqrt(diag(exit_dyad.vcov))

cbind(coef(exit_fit), exit_fit$se, coef(exit_fit)/exit_fit$se)[c(1:14,404:405),]
###

### Table 8: Parallel-trends
load("./pt_cueingresults_105.RData")
pt_fit$se <- sqrt(diag(pt_dyad.vcov))

cbind(coef(pt_fit), pt_fit$se, (coef(pt_fit)/pt_fit$se))[c(1:14,257:258),]
###